﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Data.MySqlClient;

public class DAL_tbl_qcsicomill_xrf
{
    public DAL_tbl_qcsicomill_xrf()
    {

    }



    public List<tbl_qcsicomill_xrf> select(int QC_XRF_ID)
    {
        List<tbl_qcsicomill_xrf> list = new List<tbl_qcsicomill_xrf>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select QC_XRF_ID,Work_Order_Number,DRUM_Number,Budgeted_Qty,Actual_Qty,Fe,Al,Ca,Remark,IS_ACTIVE,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_qcsicomill_xrf where QC_XRF_ID=@QC_XRF_ID";
        cmd.Parameters.AddWithValue("@QC_XRF_ID", QC_XRF_ID);
        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_xrf()
            {
                QC_XRF_ID = Convert.ToInt32(dr["QC_XRF_ID"]),
                Work_Order_Number = dr["Work_Order_Number"].ToString(),
                DRUM_Number = dr["DRUM_Number"].ToString(),
                Budgeted_Qty=Convert.ToInt32(dr["Budgeted_Qty"]),
                Actual_Qty = Convert.ToInt32(dr["Actual_Qty"]),
                Fe = Convert.ToDecimal(dr["Fe"]),
                Al = Convert.ToDecimal(dr["Al"]),
                Ca = Convert.ToDecimal(dr["Ca"]),
                Remark = dr["Remark"].ToString(),
                IS_ACTIVE = Convert.ToBoolean(dr["IS_ACTIVE"]),
                CREATED_BY = dr["CREATED_BY"].ToString(),
                CREATED_DATE = dr["CREATED_DATE"].toDateTimeWithoutError(),
                UPDATED_BY = dr["UPDATED_BY"].ToString(),
                UPDATED_DATE = dr["UPDATED_DATE"].toDateTimeWithoutError()
            });
        }

        return list;
    }



    public List<tbl_qcsicomill_xrf> selectAll()
    {
        List<tbl_qcsicomill_xrf> list = new List<tbl_qcsicomill_xrf>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select QC_XRF_ID,Work_Order_Number,DRUM_Number,Budgeted_Qty,Actual_Qty,Fe,Al,Ca,Remark,IS_ACTIVE,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_qcsicomill_xrf Order By QC_XRF_ID";

        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_xrf()
            {
                QC_XRF_ID = Convert.ToInt32(dr["QC_XRF_ID"]),
                Work_Order_Number = dr["Work_Order_Number"].ToString(),
                DRUM_Number = dr["DRUM_Number"].ToString(),
                Budgeted_Qty = Convert.ToInt32(dr["Budgeted_Qty"]),
                Actual_Qty = Convert.ToInt32(dr["Actual_Qty"]),
                Fe = Convert.ToDecimal(dr["Fe"]),
                Al = Convert.ToDecimal(dr["Al"]),
                Ca = Convert.ToDecimal(dr["Ca"]),
                Remark = dr["Remark"].ToString(),
                IS_ACTIVE = Convert.ToBoolean(dr["IS_ACTIVE"]),
                CREATED_BY = dr["CREATED_BY"].ToString(),
                CREATED_DATE = dr["CREATED_DATE"].toDateTimeWithoutError(),
                UPDATED_BY = dr["UPDATED_BY"].ToString(),
                UPDATED_DATE = dr["UPDATED_DATE"].toDateTimeWithoutError()
            });
        }

        return list;
    }

    public DataTable selectAllForGrid()
    {
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = @"Select QC_XRF_ID,Work_Order_Number,DRUM_Number,Budgeted_Qty,Actual_Qty,Fe,Al,Ca,Remark,CASE WHEN IS_ACTIVE =1 THEN 'True' ELSE 'False' END as 'IS_ACTIVE',CREATED_BY,CREATED_DATE as CREATED_DATE,UPDATED_BY,UPDATED_DATE from tbl_qcsicomill_xrf Order By QC_XRF_ID desc";

        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        return dt;
    }

    public bool insert(List<tbl_qcsicomill_xrf> list)
    {
        bool ret = false;
        List<MySqlCommand> cmds = new List<MySqlCommand>();
        string sql = @"Insert into tbl_qcsicomill_xrf(Work_Order_Number, DRUM_Number ,Budgeted_Qty ,Actual_Qty, Fe, Al, Ca, Remark, IS_ACTIVE, CREATED_BY, CREATED_DATE)
                        values(@Work_Order_Number, @DRUM_Number ,@Budgeted_Qty,@Actual_Qty, @Fe, @Al, @Ca, @Remark, @IS_ACTIVE, @CREATED_BY, @CREATED_DATE)";

        foreach (tbl_qcsicomill_xrf row in list)
        {
            MySqlCommand cmd = new MySqlCommand(sql);

            cmd.Parameters.AddWithValue("@Work_Order_Number", row.Work_Order_Number);
            cmd.Parameters.AddWithValue("@DRUM_Number", row.DRUM_Number);
            cmd.Parameters.AddWithValue("@Budgeted_Qty", row.Budgeted_Qty);
            cmd.Parameters.AddWithValue("@Actual_Qty", row.Actual_Qty);
            cmd.Parameters.AddWithValue("@Fe", row.Fe);
            cmd.Parameters.AddWithValue("@Al", row.Al);
            cmd.Parameters.AddWithValue("@Ca", row.Ca);
            cmd.Parameters.AddWithValue("@Remark", row.Remark);
            cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
            cmd.Parameters.AddWithValue("@CREATED_BY", HttpContext.Current.User.Identity.Name);
            cmd.Parameters.AddWithValue("@CREATED_DATE", DateTime.Now);
            cmds.Add(cmd);
        }
        ret = DBUtil.ExecuteNonQuery(cmds);
        return ret;
    }

    public int insert(String Work_Order_Number, String DRUM_Number, int Budgeted_Qty, int Actual_Qty, Decimal Fe, Decimal Al, Decimal Ca, String Remark, bool IS_ACTIVE, String CREATED_BY, DateTime CREATED_DATE)
    {
        int i = 0;
        string sql = @"Insert into tbl_qcsicomill_xrf(Work_Order_Number, DRUM_Number ,Budgeted_Qty ,Actual_Qty, Fe, Al, Ca, Remark, IS_ACTIVE, CREATED_BY, CREATED_DATE)
                        values(@Work_Order_Number, @DRUM_Number ,@Budgeted_Qty,@Actual_Qty, @Fe, @Al, @Ca, @Remark, @IS_ACTIVE, @CREATED_BY, @CREATED_DATE)";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@Work_Order_Number", Work_Order_Number);
        cmd.Parameters.AddWithValue("@DRUM_Number", DRUM_Number);
        cmd.Parameters.AddWithValue("@Budgeted_Qty", Budgeted_Qty);
        cmd.Parameters.AddWithValue("@Actual_Qty", Actual_Qty);
        cmd.Parameters.AddWithValue("@Fe", Fe);
        cmd.Parameters.AddWithValue("@Al", Al);
        cmd.Parameters.AddWithValue("@Ca", Ca);
        cmd.Parameters.AddWithValue("@Remark", Remark);
        cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
        cmd.Parameters.AddWithValue("@CREATED_BY", HttpContext.Current.User.Identity.Name);
        cmd.Parameters.AddWithValue("@CREATED_DATE", DateTime.Now);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }

    public bool update(List<tbl_qcsicomill_xrf> list)
    {
        bool ret = false;
        List<MySqlCommand> cmds = new List<MySqlCommand>();
        string sql = @"Update tbl_qcsicomill_xrf SET Work_Order_Number=@Work_Order_Number, DRUM_Number=@DRUM_Number, Budgeted_Qty=@Budgeted_Qty, Actual_Qty=@Actual_Qty, Fe=@Fe, Al=@Al, Ca=@Ca, Remark=@Remark, IS_ACTIVE=@IS_ACTIVE, UPDATED_BY=@UPDATED_BY, UPDATED_DATE=@UPDATED_DATE WHERE QC_XRF_ID=@QC_XRF_ID";

        foreach (tbl_qcsicomill_xrf row in list)
        {
            MySqlCommand cmd = new MySqlCommand(sql);

            cmd.Parameters.AddWithValue("@QC_XRF_ID", row.QC_XRF_ID);
            cmd.Parameters.AddWithValue("@Work_Order_Number", row.Work_Order_Number);
            cmd.Parameters.AddWithValue("@DRUM_Number", row.DRUM_Number);
            cmd.Parameters.AddWithValue("@Budgeted_Qty", row.Budgeted_Qty);
            cmd.Parameters.AddWithValue("@Actual_Qty", row.Actual_Qty);
            cmd.Parameters.AddWithValue("@Fe", row.Fe);
            cmd.Parameters.AddWithValue("@Al", row.Al);
            cmd.Parameters.AddWithValue("@Ca", row.Ca);
            cmd.Parameters.AddWithValue("@Remark", row.Remark);
            cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
            cmd.Parameters.AddWithValue("@UPDATED_BY", HttpContext.Current.User.Identity.Name);
            cmd.Parameters.AddWithValue("@UPDATED_DATE", DateTime.Now);

            cmds.Add(cmd);
        }
        ret = DBUtil.ExecuteNonQuery(cmds);
        return ret;
    }


    public int update(int QC_XRF_ID, String Work_Order_Number, String DRUM_Number, int Budgeted_Qty, int Actual_Qty, Decimal Fe, Decimal Al, Decimal Ca, String Remark, bool IS_ACTIVE, String UPDATED_BY, DateTime UPDATED_DATE)
    {
        int i = 0;
        string sql = @"Update tbl_qcsicomill_xrf SET Work_Order_Number=@Work_Order_Number, DRUM_Number=@DRUM_Number, Budgeted_Qty=@Budgeted_Qty, Actual_Qty=@Actual_Qty, Fe=@Fe, Al=@Al, Ca=@Ca, Remark=@Remark, IS_ACTIVE=@IS_ACTIVE, UPDATED_BY=@UPDATED_BY, UPDATED_DATE=@UPDATED_DATE WHERE QC_XRF_ID=@QC_XRF_ID";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@QC_XRF_ID", QC_XRF_ID);
        cmd.Parameters.AddWithValue("@Work_Order_Number", Work_Order_Number);
        cmd.Parameters.AddWithValue("@DRUM_Number", DRUM_Number);
        cmd.Parameters.AddWithValue("@Budgeted_Qty", Budgeted_Qty);
        cmd.Parameters.AddWithValue("@Actual_Qty", Actual_Qty);
        cmd.Parameters.AddWithValue("@Fe", Fe);
        cmd.Parameters.AddWithValue("@Al", Al);
        cmd.Parameters.AddWithValue("@Ca", Ca);
        cmd.Parameters.AddWithValue("@Remark", Remark);
        cmd.Parameters.AddWithValue("@IS_ACTIVE", true);
        cmd.Parameters.AddWithValue("@UPDATED_BY", HttpContext.Current.User.Identity.Name);
        cmd.Parameters.AddWithValue("@UPDATED_DATE", DateTime.Now);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }



    public int delete(int QC_XRF_ID)
    {
        int i = 0;
        string sql = "Delete FROM tbl_qcsicomill_xrf WHERE QC_XRF_ID=@QC_XRF_ID";

        MySqlCommand cmd = new MySqlCommand(sql);

        cmd.Parameters.AddWithValue("@QC_XRF_ID", QC_XRF_ID);

        i = DBUtil.ExecuteNonQuery(cmd);
        return i;
    }

    public List<tbl_qcsicomill_xrf> ddlWorkOrderForLeco_carbon()
    {
        List<tbl_qcsicomill_xrf> list = new List<tbl_qcsicomill_xrf>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select distinct Work_Order_Number from tbl_qcsicomill_xrf Order By Work_Order_Number";

        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_xrf()
            {
               
                Work_Order_Number = dr["Work_Order_Number"].ToString(),
               
            });
        }

        return list;
    }

    public List<tbl_qcsicomill_xrf> ddlDrumNumber(string WorkOrderNumber)
    {
        List<tbl_qcsicomill_xrf> list = new List<tbl_qcsicomill_xrf>();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "Select distinct Drum_Number from tbl_qcsicomill_xrf where Work_Order_Number=@Work_Order_Number and Drum_Number<>'' order by Drum_Number";
        cmd.Parameters.AddWithValue("@Work_Order_Number", WorkOrderNumber);
        DataTable dt = DBUtil.ExecuteDataTable(cmd);
        foreach (DataRow dr in dt.Rows)
        {
            list.Add(new tbl_qcsicomill_xrf()
            {
                DRUM_Number = dr["Drum_Number"].ToString()
            });
        }

        return list;
    }
}